/**     
 * @Title: UserController.java   
 * @Package com.yitong.controller   
 * @Description: TODO
 * @author weiwei 
 * @date 2017年7月20日 下午3:36:21   
 * @version V1.0     
 */
package com.yitong.controller;

import java.util.Collections;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yitong.entity.User;
import com.yitong.service.UserService;
import com.yitong.spring.annotation.ReqJson;
import com.yitong.utils.ConstantUtils;
import com.yitong.utils.PropertyUtils;
import com.yitong.utils.RetCode;
import com.yitong.utils.RetResult;
import com.yitong.utils.RspUtils;
import com.yitong.utils.SessionUtils;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author weiwei
 * @date 2017年7月20日 下午3:36:21
 * 
 */
@Controller
@RequestMapping("v1/user/")
@EnableAutoConfiguration
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "queryByName", method = RequestMethod.POST, produces = ConstantUtils.CONTENT_TYPE_APPLICATION_JSON)
	@ResponseBody
	public Object findByUserName(@ReqJson String name) {
		List<User> dataList = userService.findByUserName(name);
		return RspUtils.makeRsp((null == dataList || dataList.isEmpty()) ? Collections.emptyList() : dataList);
	}

	/**
	 * @Title: login
	 * @Description: 登入
	 * @param email
	 * @param passwd
	 * @return Object
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST, produces = ConstantUtils.CONTENT_TYPE_APPLICATION_JSON)
	@ResponseBody
	public Object login(@ReqJson String email, @ReqJson String passwd) {
		RetResult retResult = userService.findByEmailAndPassword(email, passwd);
		Object obj = retResult.getData(PropertyUtils.PROPERTY_USER_DATA_KEY);
		return RspUtils.makeRsp(retResult, obj == null ? new Object() : obj);
	}

	/**
	 * @Title: logout
	 * @Description: 登出
	 * @param userId
	 * @return Object
	 */
	@RequestMapping(value = "logout", method = RequestMethod.POST, produces = ConstantUtils.CONTENT_TYPE_APPLICATION_JSON)
	@ResponseBody
	public Object logout(@ReqJson String userId) {
		Object rs = SessionUtils.removeAuthcToken();
		SecurityUtils.getSubject().logout();
		if (rs != null) {
			return RspUtils.makeOkRsp();
		}
		return RspUtils.makeRsp(RetCode.LOGOUT_FAIL, "登出失败");
	}
}
